package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.userInfo.entity.AtMineMessage;
import com.rongshu.view.userInfo.listener.OnMsgFunctionClickListener;
import com.utils.StringUtils;
import com.widget.CircleImageView;
import com.widget.SwipeMenuView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AtMineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //context
    private Context mContext = null;
    //list
    private List<AtMineMessage.ResponseBean> atMineMessageList = null;
    //interface
    private OnMsgFunctionClickListener listener = null;

    public AtMineAdapter(Context context, List<AtMineMessage.ResponseBean> atMeList) {
        this.mContext = context;
        this.atMineMessageList = atMeList;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_at_mine_item_view, parent, false);
        return new AtMineViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        final AtMineMessage.ResponseBean responseBean = atMineMessageList.get(position);
        final AtMineViewHolder holder = (AtMineViewHolder) viewHolder;
        try {
            //头像
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.user_head_icon);
            Glide.with(mContext)
                    .load(responseBean.getAvatar())
                    .apply(options)
                    .into(holder.iv_head_icon);
            holder.iv_vip.setVisibility(responseBean.isVip() ? View.VISIBLE : View.GONE);
            //来自人
            holder.txt_frombi_name.setText(responseBean.getFrombi_name() + " 在留言中提到了你");
            //提示
            holder.iv_hint.setVisibility(responseBean.isIs_read() ? View.GONE : View.VISIBLE);
            //日期
            String time = responseBean.getCreate_datetime();
            holder.txt_at_time.setText(DateUtils.ConvertDate(time));
            //内容
            String content = responseBean.getContents();
            holder.txt_reply_contents.setText(StringUtils.decoderMsg(content));
            //文章title
            AtMineMessage.ResponseBean.ObjListBean bean = responseBean.getObj_list();
            if (bean != null && !TextUtils.isEmpty(bean.getTitle())) {
                holder.txt_article_title.setText(bean.getTitle());
            }
            //头像点击
            holder.iv_head_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onUserHeaderClick(responseBean.getFrom_uid());
                    }
                }
            });
            //listener
            holder.layout_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                }
            });

            //delete
            holder.btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        holder.swipemenu_view.quickClose();
                        listener.onItemDeleteClick(position);
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return atMineMessageList == null ? 0 : atMineMessageList.size();
    }

    public static class AtMineViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_content)
        RelativeLayout layout_content;
        @BindView(R.id.iv_head_icon)
        CircleImageView iv_head_icon;
        @BindView(R.id.iv_vip)
        ImageView iv_vip;
        @BindView(R.id.txt_frombi_name)
        TextView txt_frombi_name;
        @BindView(R.id.txt_at_time)
        TextView txt_at_time;
        @BindView(R.id.txt_reply_contents)
        TextView txt_reply_contents;
        @BindView(R.id.txt_article_title)
        TextView txt_article_title;
        @BindView(R.id.iv_hint)
        ImageView iv_hint;
        @BindView(R.id.swipemenu_view)
        SwipeMenuView swipemenu_view;
        @BindView(R.id.btn_delete)
        TextView btn_delete;

        public AtMineViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnMsgFunctionClickListener listener) {
        this.listener = listener;
    }
}
